/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package backend;

import com.mysql.jdbc.Messages;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import db.UserDAO;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.faces.validator.ValidatorException;

/**
 *
 * @author brook
 */
@ManagedBean(name = "RegisterBean")
@RequestScoped
public class RegisterBean {

    public String email;
    public String firstname;
    public String lastname;
    public String password;
    public int authority;
    public String startDate;
    public double hourlyRate;
    public String address;
    public String ssn;
    Messages messages;
    
    public String getStartDate() {
        return startDate;
    }

    public void setStartDate(String startDate) {
        this.startDate = startDate;
    }

    public double getHourlyRate() {
        return hourlyRate;
    }

    public void setHourlyRate(double hourlyRate) {
        this.hourlyRate = hourlyRate;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getSsn() {
        return ssn;
    }

    public void setSsn(String ssn) {
        this.ssn = ssn;
    }
    

    public int getAuthority() {
        return authority;
    }

    public void setAuthority(int authority) {
        this.authority = authority;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getFirstname() {
        return firstname;
    }

    public void setFirstname(String firstname) {
        this.firstname = firstname;
    }

    public String getLastname() {
        return lastname;
    }

    public void setLastname(String lastname) {
        this.lastname = lastname;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public RegisterBean() {
    }
    
    public void registerCustomer() throws Exception{
        UserDAO user = UserDAO.createUser(email, firstname, lastname, password, 0);
        if (user != null) {
            FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put("user", user);
            FacesContext.getCurrentInstance().getExternalContext().redirect("./app/home");
        }

    }
    public void registerEmployee() throws Exception{
        UserDAO employee = UserDAO.createEmployee(firstname, lastname,email, password,startDate,hourlyRate, address,ssn, 1);
        FacesContext facesContext = FacesContext.getCurrentInstance();
        if (employee != null) {
            facesContext.addMessage("loginForm", new FacesMessage("Registed"));
        }
        else{
            facesContext.addMessage("loginForm", new FacesMessage("Hourly Rate should be numeric format."));
            facesContext.addMessage("loginForm", new FacesMessage("SSN should be 9 digit."));
            facesContext.addMessage("loginForm", new FacesMessage("Start Date should be yyyy-mm-dd format."));
        }
        
    }

}
